package org.msb.accentis.data.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;

import org.msb.accentis.data.DataManagerFactory;
import org.msb.accentis.utils.HashUtil;

/**
 * The {@code BaseObject} class is the base class of all model objects. The class provides the ability to retrieve and set a unique identifier for the object.
 * 
 * @author Marc Boudreau
 * 
 * @since 1.0
 */
@MappedSuperclass
public abstract class BaseObject {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	/**
	 * Returns the unique identifier of this object as an {@code int} value.
	 * 
	 * @return The id value as an {@code int} value.
	 */
	public int getId() {
		return id;
	}

	public boolean isSpecial() {
		return false;
	}

	@Override
	public boolean equals(Object obj) {
		if (super.equals(obj))
			return true;
		else if (obj instanceof BaseObject)
			return getId() == ((BaseObject) obj).getId();

		return false;
	}

	@Override
	public int hashCode() {
		return HashUtil.hash(getId());
	}

	@Override
	public String toString() {
		return getClass() + "-" + getId(); //$NON-NLS-1$
	}
	
	@PostPersist
	public void objectCreated() {
		DataManagerFactory.getInstance().getDatabase().getObjectNotifier().objectCreated(this);
	}
	
	@PostRemove
	public void objectDeleted() {
		DataManagerFactory.getInstance().getDatabase().getObjectNotifier().objectDeleted(this);
	}
	
	@PostUpdate
	public void objectSaved() {
		DataManagerFactory.getInstance().getDatabase().getObjectNotifier().objectSaved(this);
	}
	
	@PostLoad
	public void objectRefreshed() {
		DataManagerFactory.getInstance().getDatabase().getObjectNotifier().objectRefreshed(this);
	}
}